
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 10/07/2013 13:21:54
-- Generated from EDMX file: C:\Users\mje\itwem\TemperatureRest\TemperatureDatabase\TemperatureDB.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [TemperatureDB];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_SensorMeasurement]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Measurement] DROP CONSTRAINT [FK_SensorMeasurement];
GO
IF OBJECT_ID(N'[dbo].[FK_RegulatorSensor]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Regulator] DROP CONSTRAINT [FK_RegulatorSensor];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Measurement]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Measurement];
GO
IF OBJECT_ID(N'[dbo].[Sensor]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Sensor];
GO
IF OBJECT_ID(N'[dbo].[Regulator]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Regulator];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Measurement'
CREATE TABLE [dbo].[Measurement] (
    [SensorId] int  NOT NULL,
    [Id] int IDENTITY(1,1) NOT NULL,
    [Temperature] float  NOT NULL,
    [Timestamp] datetime  NOT NULL
);
GO

-- Creating table 'Sensor'
CREATE TABLE [dbo].[Sensor] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] varchar(max)  NOT NULL,
    [Place] varchar(max)  NOT NULL,
    [Mac] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'Regulator'
CREATE TABLE [dbo].[Regulator] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [TargetTemperature] float  NOT NULL,
    [Active] bit  NOT NULL,
    [Mac] nvarchar(max)  NOT NULL,
    [Sensor_Id] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'Measurement'
ALTER TABLE [dbo].[Measurement]
ADD CONSTRAINT [PK_Measurement]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Sensor'
ALTER TABLE [dbo].[Sensor]
ADD CONSTRAINT [PK_Sensor]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Regulator'
ALTER TABLE [dbo].[Regulator]
ADD CONSTRAINT [PK_Regulator]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [SensorId] in table 'Measurement'
ALTER TABLE [dbo].[Measurement]
ADD CONSTRAINT [FK_SensorMeasurement]
    FOREIGN KEY ([SensorId])
    REFERENCES [dbo].[Sensor]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_SensorMeasurement'
CREATE INDEX [IX_FK_SensorMeasurement]
ON [dbo].[Measurement]
    ([SensorId]);
GO

-- Creating foreign key on [Sensor_Id] in table 'Regulator'
ALTER TABLE [dbo].[Regulator]
ADD CONSTRAINT [FK_RegulatorSensor]
    FOREIGN KEY ([Sensor_Id])
    REFERENCES [dbo].[Sensor]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_RegulatorSensor'
CREATE INDEX [IX_FK_RegulatorSensor]
ON [dbo].[Regulator]
    ([Sensor_Id]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------